package sources;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
 * A class that runs the Add TimeZone GUI and behind the scene code.
 *
 * @author Alana Meditz
 * @author Ty Yago
 * @author Katherine Kishaba
 * @author Jimmy CUmming
 * 
 */

public class AddTimeZoneFrame extends AddComponentFrame {
	private static final int WIDTH = 550;
	private static final int HEIGHT = 400;
	
	private ICalendar iCal;
	
	private JLabel tzidL;
	//private JTextField tzidTF;
	private JComboBox timezonesCB;
	private JButton addB, backB;
	private AddButtonHandler addBH;
	private BackButtonHandler backBH;
	
	/**
	 * AddTimeZoneFrame constructor.
	 * Creates the GUI allowing users to add timezone components to the given ICalendar object. 
	 * @param iCal The ICalendar to which timezone components will be added.
	 */
	public AddTimeZoneFrame(ICalendar iCal) {
		this.iCal = iCal;
		
		setTitle("Select best Time Zone");
		setUpFrame();
		
		instantiateLabels();
		instantiateComboBoxes();
		//instantiateTextFields();
		instantiateButtons();
		
		setUpPane();
	}
		
	/**
	 * Helper function used by constructor to label input boxes.
	 */
	private void instantiateLabels() {
		tzidL = new JLabel("Select time zone*: ", SwingConstants.RIGHT);
	}
	
	/**
	 * Helper function used by constructor to instantiate JComboBox objects
	 */
	private void instantiateComboBoxes() {
		timezonesCB = new JComboBox(timezones);
		timezonesCB.setEditable(false);
	}
	
	/**
	 * Helper function used by constructor to create text fields for user input.
	 */
//	private void instantiateTextFields() {
//		tzidTF = new JTextField(30);
//	}
	
	/**
	 * Helper function used by constructor to create buttons and implement their event handlers.
	 */
	private void instantiateButtons() {
		addB = new JButton("Add");
		addBH = new AddButtonHandler();
		addB.addActionListener(addBH);
		backB = new JButton("Back");
		backBH = new BackButtonHandler();
		backB.addActionListener(backBH);
	}
	
	/**
	 * Helper function used by constructor to declare pane properties and add Frame elements to the
	 * ContentPane of this frame for display.
	 */
	private void setUpPane() {
		Container pane = getContentPane();
		pane.setLayout(new GridLayout(2, 2));
		
		// Add items to pane
		pane.add(tzidL);
		//pane.add(tzidTF);
		pane.add(timezonesCB);
		pane.add(addB);
		pane.add(backB);
	}
	
	/**
	 * 
	 * Event handler for the Add Button.
	 *
	 */
	private class AddButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String tzid;
			
			tzid = (String) timezonesCB.getSelectedItem();
			
			ICalComponent component = new ICalComponent("VTIMEZONE");
			if(tzid.equalsIgnoreCase("UTC-11 (Pacific/Midway)")) {
				tzid = "Pacific/Midway";
				offset = -11;
			}
			else if(tzid.equalsIgnoreCase("UTC-10 (Pacific/Hawaii)")) {
				tzid = "Pacific/Hawaii";
				AddComponentFrame.offset = -10;
			}
			else if(tzid.equalsIgnoreCase("UTC-9 (America/Anchorage)")) {
				tzid = "America/Anchorage";
				offset = -9;
			}
			else if(tzid.equalsIgnoreCase("UTC-8 (America/Los Angeles)")) {
				tzid = "America/Los Angeles";
				offset = -8;
			}
			else if(tzid.equalsIgnoreCase("UTC-7 (America/Denver)")) {
				tzid = "America/Denver";
				offset = -7;
			}
			else if(tzid.equalsIgnoreCase("UTC-6 (America/Chicago)")) {
				tzid = "America/Chicago";
				offset = -6;
			}
			else if(tzid.equalsIgnoreCase("UTC-5 (America/New York)")) {
				tzid = "America/New York";
				offset = -5;
			}
			else if(tzid.equalsIgnoreCase("UTC-4 (America/Halifax)")) {
				tzid = "America/Halifax";
				offset = -4;
			}
			else if(tzid.equalsIgnoreCase("UTC-3 (America/Argentina/Buenos Aires)")) {
				tzid = "America/Argentina/Buenos Aires";
				offset = -3;
			}
			else if(tzid.equalsIgnoreCase("UTC-2 (America/Noronha)")) {
				tzid = "America/Noronha";
				offset = -2;
			}
			else if(tzid.equalsIgnoreCase("UTC-1 (America/Azores)")) {
				tzid = "America/Azores";
				offset = -1;
			}
			else if(tzid.equalsIgnoreCase("UTC")) {
				tzid = "UTC";
				offset = 0;
			}
			else if(tzid.equalsIgnoreCase("UTC+1 (Europe/Madrid)")) {
				tzid = "Europe/Madrid";
				offset = 1;
			}
			else if(tzid.equalsIgnoreCase("UTC+2 (Europe/Helsinki)")) {
				tzid = "Europe/Helsinki";
				offset = 2;
			}
			else if(tzid.equalsIgnoreCase("UTC+3 (Asia/Kuwait)")) {
				tzid = "Asia/Kuwait";
				offset = 3;
			}
			else if(tzid.equalsIgnoreCase("UTC+4 (Europe/Moscow)")) {
				tzid = "Europe/Moscow";
				offset = 4;
			}
			else if(tzid.equalsIgnoreCase("UTC+5 (Asia/Kabul)")) {
				tzid = "Asia/Kabul";
				offset = 5;
			}
			else if(tzid.equalsIgnoreCase("UTC+6 (Asia/Dhaka)")) {
				tzid = "Asia/Dhaka";
				offset = 6;
			}
			else if(tzid.equalsIgnoreCase("UTC+7 (Asia/Bangkok)")) {
				tzid = "Asia/Bangkok";
				offset = 7;
			}
			else if(tzid.equalsIgnoreCase("UTC+8 (Asia/Manila)")) {
				tzid = "Asia/Manila";
				offset = 8;
			}
			else if(tzid.equalsIgnoreCase("UTC+9 (Asia/Tokyo)")) {
				tzid = "Asia/Tokyo";
				offset = 9;
			}
			else if(tzid.equalsIgnoreCase("UTC+10 (Australia/Sydney)")) {
				tzid = "Australia/Sydney";
				offset = 10;
			}
			else if(tzid.equalsIgnoreCase("UTC+11 (Antarctica/Macquarie)")) {
				tzid = "Antarctica/Macquarie";
				offset = 11;
			}
			else if(tzid.equalsIgnoreCase("UTC+12 (Pacific/Auckland)")) {
				tzid = "Pacific/Auckland";
				offset = 12;
			}
			else {
				tzid = "UTC"; // default to UTC if no selection
				AddComponentFrame.offset = 0;
			}
			
			component.addComponentProperty("TZID", tzid);
			
			iCal.addComponent(component);
		}
	}
}